/*
 * Copyright (c) 田梓萱 2021-2022.
 * All Rights Reserved.
 * All codes are protected by China's regulations on the protection of computer software, and infringement must be investigated.
 * 版权所有 (c) 田梓萱 2021-2022.
 * 所有代码均受中国《计算机软件保护条例》保护，本项目已获得软著.
 */

package com.xcl.calculator.slice.BianQian.second;

import com.xcl.calculator.Preference_RW;
import com.xcl.calculator.ResourceTable;
import com.xcl.calculator.XLog;
import com.xcl.calculator.slice.BianQian.Contactor;
import ohos.agp.components.*;
import ohos.app.Context;
import ohos.distributedhardware.devicemanager.DeviceInfo;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.List;

/**
 * The type Contact provider 1.
 *
 * @author Xcl
 * @date 2021 /1/12
 * @package com.xcl.calculator.slice.BianQian.second
 */
public class ContactProvider1 extends BaseItemProvider {
    /**
     * The constant label.
     */
    private static final HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, 0x00234, "ContactProvider1");
    /**
     * The Contact arrays.
     */
    private final List<Contactor> contactArrays;

    /**
     * The Context.
     */
    private final Context context;

    /**
     * The Adapter click listener.
     */
    private AdapterClickListener adapterClickListener;

    /**
     * 实例化
     *
     * @param context       the context
     * @param contactArrays the contact arrays
     */
    ContactProvider1(Context context, List<Contactor> contactArrays) {
        this.context = context;
        this.contactArrays = contactArrays;
    }

    /**
     * 其它
     *
     * @return count count
     */
    @Override
    public int getCount() {
        return this.contactArrays == null ? 0 : this.contactArrays.size();
    }

    /**
     * 其它
     *
     * @param position the position
     * @return item item
     */
    @Override
    public Object getItem(int position) {
        if (position < this.contactArrays.size() && position >= 0) return this.contactArrays.get(position);
        return null;
    }

    /**
     * 其它
     *
     * @param position the position
     * @return item id
     */
    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * 其它
     *
     * @param position           the position
     * @param componentPara      the component para
     * @param componentContainer the component container
     * @return component component
     */
    @Override
    public Component getComponent(int position, Component componentPara, ComponentContainer componentContainer) {
        Component component = componentPara;
        try {
            ViewHolder viewHolder = null;
            if (component == null) {
                if (Preference_RW.getLocalDeviceType() == DeviceInfo.DeviceType.PHONE.value()) {
                    component = LayoutScatter.getInstance(this.context).parse(ResourceTable.Layout_bianqianshow_1m,
                            null, false);
                    XLog.info(label, "当前设备类型为手机");
                } else if (Preference_RW.getLocalDeviceType() == DeviceInfo.DeviceType.TABLET.value()) {
                    component = LayoutScatter.getInstance(this.context).parse(ResourceTable.Layout_bianqianshow_1t,
                            null, false);
                    XLog.info(label, "当前设备类型为平板");
                } else XLog.warn(label, "暂不支持您的设备:(" + Preference_RW.getLocalDeviceType() + ")");
                viewHolder = new ViewHolder();
                Component componentText = component.findComponentById(ResourceTable.Id_titles1);
                if (componentText instanceof Text) viewHolder.titles = (Text) componentText;
                componentText = component.findComponentById(ResourceTable.Id_messages1);
                if (componentText instanceof Text) viewHolder.messages = (Text) componentText;
                viewHolder.share = (Button) component.findComponentById(ResourceTable.Id_sharecontact);
                viewHolder.show = (Button) component.findComponentById(ResourceTable.Id_bqshow1);
                viewHolder.delete = (Button) component.findComponentById(ResourceTable.Id_delete1);
                viewHolder.edit = (Button) component.findComponentById(ResourceTable.Id_edit1);
                component.setTag(viewHolder);
            } else if (component.getTag() instanceof ViewHolder) viewHolder = (ViewHolder) component.getTag();
            if (viewHolder != null) {
                viewHolder.titles.setText(this.contactArrays.get(position).getTitles());
                viewHolder.messages.setText(this.contactArrays.get(position).getMessages());
                viewHolder.share.setClickedListener(new Component.ClickedListener() {
                    @Override
                    public void onClick(Component deleteComponent) {
                        if (ContactProvider1.this.adapterClickListener != null)
                            ContactProvider1.this.adapterClickListener.share(position);
                    }
                });
                viewHolder.show.setClickedListener(new Component.ClickedListener() {
                    @Override
                    public void onClick(Component deleteComponent) {
                        if (ContactProvider1.this.adapterClickListener != null)
                            ContactProvider1.this.adapterClickListener.show(position);
                    }
                });
                viewHolder.edit.setClickedListener(new Component.ClickedListener() {
                    @Override
                    public void onClick(Component editComponent) {
                        if (ContactProvider1.this.adapterClickListener != null)
                            ContactProvider1.this.adapterClickListener.edit(position);
                    }
                });
                viewHolder.delete.setClickedListener(new Component.ClickedListener() {
                    @Override
                    public void onClick(Component deleteComponent) {
                        if (ContactProvider1.this.adapterClickListener != null)
                            ContactProvider1.this.adapterClickListener.delete(position);
                    }
                });
            }
        } catch (Exception e) {
            XLog.error(label, e.getMessage());
        }
        return component;
    }

    /**
     * 其它
     *
     * @param adapterClickListener the adapter click listener
     */
    void setAdapterClickListener1(AdapterClickListener adapterClickListener) {
        this.adapterClickListener = adapterClickListener;
    }

    /**
     * 其它
     */
    public interface AdapterClickListener {
        /**
         * Edit.
         *
         * @param position the position
         */
        void edit(int position);

        /**
         * Delete.
         *
         * @param position the position
         */
        void delete(int position);

        /**
         * Show.
         *
         * @param position the position
         */
        void show(int position);

        /**
         * Share.
         *
         * @param position the position
         */
        void share(int position);
    }

    /**
     * 其它
     */
    private static class ViewHolder {
        /**
         * The Titles.
         */
        private Text titles;
        /**
         * The Messages.
         */
        private Text messages;
        /**
         * The Share.
         */
        private Button share;
        /**
         * The Show.
         */
        private Button show;
        /**
         * The Edit.
         */
        private Button edit;
        /**
         * The Delete.
         */
        private Button delete;
    }
}
